//
//  LSHttpEnclosureTool.m
//  LookScore
//
//  Created by wr on 2018/1/17.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSHttpEnclosureTool.h"
#import "LSHttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "LSReturnFormat.h"
#import "LSAccountArchive.h"
//#import "LSHttpParameter.h"

//static CFAbsoluteTime start;
//static CFAbsoluteTime end;
@implementation LSHttpEnclosureTool
{
    CFAbsoluteTime end;
}

/**
 GET请求
 
 @param URLString URLString description
 @param parameters <#parameters description#>
 @param resBlock <#resBlock description#>
 */
+(void)GET:(NSString *)URLString parameters:(id)parameters resBlock:(resBlock)resBlock{

//    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
////    hud.label.text=@"正在请求中...";
//    hud.mode=MBProgressHUDModeIndeterminate;
//    [hud showAnimated:YES];
    
    
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    // do something
    
    
    
    
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [LSHttpTool GET:URLString parameters:parameters success:^(id responseObject) {
//        [hud hideAnimated:YES];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        LSReturnFormat *format=[LSReturnFormat mj_objectWithKeyValues:responseObject];
//          NSString *sdsad=[responseObject mj_JSONString];
        //           userToken 会时时刷新，判断为@“” 即为掉线状态
        if ([CommonClass isBlankString:format.userToken]) {
            [LSAccountArchive autoLoginSuccessStatus:NO];
            [LSAccountArchive logout];
        }
        [LSAccountArchive saveUserToken:format.userToken];
        if (format.status==0) {
            NSArray *array=format.models;
            if (array==nil) {
                [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
                resBlock(array);
            }else{
                resBlock(format.models);
            }
        }else{
           [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
            resBlock(nil);
        }
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
         MyLog(@"请求耗时 -- %f", end - start);
    } failure:^(NSError *error) {
//        [hud hideAnimated:YES];
        MyLog(@"%@",error);
        resBlock(nil);
        [MBProgressHUD showOnlyMessage:error.localizedDescription afterDelay:1.5f];
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        MyLog(@"请求耗时 -- %f", end - start);
    }];
    
   
}



/**
 POST请求
 
 @param URLString <#URLString description#>
 @param parameters <#parameters description#>
 @param resBlock <#resBlock description#>
 */
+(void)POST:(NSString *)URLString parameters:(id)parameters resBlock:(resBlock)resBlock{
    
//    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
////    hud.label.text=@"正在请求中...";
//    hud.mode=MBProgressHUDModeIndeterminate;
//    [hud showAnimated:YES];
    
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    [LSHttpTool Post:URLString parameters:parameters success:^(id responseObject) {
//        [hud hideAnimated:YES];
        LSReturnFormat *format=[LSReturnFormat mj_objectWithKeyValues:responseObject];
        if ([CommonClass isBlankString:format.userToken]) {
            [LSAccountArchive autoLoginSuccessStatus:NO];
            [LSAccountArchive logout];
        }
        [LSAccountArchive saveUserToken:format.userToken];
        if (format.status==0) {
            NSArray *array=format.models;
            if (array==nil) {
//                [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
                resBlock(array);
            }else{
//                [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
                resBlock(format.models);
            }
        }else{
            [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
            resBlock(nil);
        }
        
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        MyLog(@"请求耗时 -- %f", end - start);
       
    } failure:^(NSError *error) {
//        [hud hideAnimated:YES];
        
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        MyLog(@"请求耗时 -- %f", end - start);
        
        resBlock(nil);
        [MBProgressHUD showOnlyMessage:error.localizedDescription afterDelay:1.5f];
    }];
}

/**
 Upload 图片

 */
+(void)Upload:(NSString *)URLString parameters:(id)parameters uploadParamArray:(NSArray *)uploadParamArray  resBlock:(resBlock)resBlock{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text=@"正在上传中...";
    hud.mode=MBProgressHUDModeIndeterminate;
    
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    [hud showAnimated:YES];
    [LSHttpTool Upload:URLString parameters:parameters uploadParamArray:uploadParamArray success:^(id responseObject) {
        [hud hideAnimated:YES];
//        NSString *sdsad=[responseObject mj_JSONString];
        LSReturnFormat *format=[LSReturnFormat mj_objectWithKeyValues:responseObject];
        if ([CommonClass isBlankString:format.userToken]) {
            [LSAccountArchive autoLoginSuccessStatus:NO];
            [LSAccountArchive logout];
        }
        [LSAccountArchive saveUserToken:format.userToken];
        if (format.status==0) {
            NSArray *array=format.models;
            if (array==nil) {
                [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
                resBlock(array);
            }else{
                [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
                resBlock(format.models);
            }
        }else{
            [MBProgressHUD showOnlyMessage:format.msg afterDelay:1.5f];
            resBlock(nil);
        }
        
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        MyLog(@"请求耗时 -- %f", end - start);
        
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
        
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        MyLog(@"请求耗时 -- %f", end - start);
        
        resBlock(nil);
        [MBProgressHUD showOnlyMessage:error.localizedDescription afterDelay:1.5f];
    }];
}
#pragma mark 🔥 更新ApiDomain
+(void)updateApiDomain{
    [LSAccountArchive saveApiDomain:LSHostURL];
    [LSHttpEnclosureTool GET:LSApiDomain parameters:nil resBlock:^(id responseObject) {
        if (responseObject) {
            NSString *apiDomainString=(NSString *)responseObject;
            [LSAccountArchive saveApiDomain:apiDomainString];
        }
    }];
}



@end
